package com.systemic.service.impl;

import com.systemic.mapper.CommentMapper;
import com.systemic.mapper.DynamicMapper;
import com.systemic.pojo.Comment;
import com.systemic.pojo.Dynamic;
import com.systemic.service.CommentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: CommentServiceImpl
 * @Description:
 * @Author:
 * @CreateTime: 2024-01-20 16:39
 * @Version:
 **/
@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;
    @Resource
    private DynamicMapper dynamicMapper;

    /**
     * 评论动态
     * @param comment
     * @return
     */
    @Override
    @Transactional
    public int create(Comment comment) {
        int row = 0;
        // 根据评论id查询评论，拿到动态id 对动态做更新
        Dynamic dynamic = dynamicMapper.selectById(comment.getDid());
        if (dynamic!=null){
            if (dynamic.getCommentNum()==null){
                dynamic.setCommentNum(1);
            } else if (dynamic.getCommentNum()==0){
                dynamic.setCommentNum(1);
            } else {
                dynamic.setCommentNum(dynamic.getCommentNum()+1);
            }
            comment.setCommentDate(new Date());
            row = dynamicMapper.update(dynamic);
            row = commentMapper.insert(comment) + row;

        }
        if (row == 0){
            throw new RuntimeException("新增评论失败");
        }
        return row;
    }

    /**
     * 回复
     * @param comment
     * @return
     */
    @Override
    @Transactional
    public int reply(Comment comment) {
        int row = 0;
        // 根据评论id查询评论，拿到动态id 对动态做更新
        Dynamic dynamic = dynamicMapper.selectById(comment.getDid());
        if (dynamic!=null){
            if (dynamic.getCommentNum()==null){
                dynamic.setCommentNum(1);
            } else if (dynamic.getCommentNum()==0){
                dynamic.setCommentNum(1);
            } else {
                dynamic.setCommentNum(dynamic.getCommentNum()+1);
            }
            comment.setCommentDate(new Date());
            row = dynamicMapper.update(dynamic);
            row = commentMapper.insert(comment) + row;
        }

        if (row == 0){
            throw new RuntimeException("新增评论失败");
        }
        return row;
    }

    /**
     * 评论区视图
     * @param did
     * @return
     */
    @Override
    public List<Comment> getView(Integer did) {
        Comment comment = new Comment();
        comment.setDid(did);
        return commentMapper.select(comment);
    }

    /**
     * 删除评论
     * @param id
     * @return
     */
    @Override
    @Transactional
    public int delete(Integer id) {
        int row = 0;
        Comment comment = new Comment();
        comment.setId(id);
        Comment res = commentMapper.selectById(id);
        // 根据评论id查询评论，拿到动态id 对动态做更新
        Dynamic dynamic = dynamicMapper.selectById(res.getDid());
        if (dynamic!=null){
            if (dynamic.getCommentNum()!=null || dynamic.getCommentNum()==0){
                dynamic.setCommentNum(dynamic.getCommentNum()-1);
                row = dynamicMapper.update(dynamic);
                row = commentMapper.delete(comment) + row;
            } else {
                throw new RuntimeException("没有评论，无需删除");
            }
        }
        return row;
    }


    /**
     * 批量删除评论
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public int deleteList(Integer[] ids) {
        int row = 0;
        // 删除话题的动态关联、关注用户
        if (ids.length!=0){
            for (Integer id:ids) {
                Comment comment = new Comment();
                comment.setId(id);
                Comment res = commentMapper.selectById(id);
                // 根据评论id查询评论，拿到动态id 对动态做更新
                Dynamic dynamic = dynamicMapper.selectById(res.getDid());
                if (dynamic!=null){
                    if (dynamic.getCommentNum()!=null || dynamic.getCommentNum()==0){
                        dynamic.setCommentNum(dynamic.getCommentNum()-1);
                        row = dynamicMapper.update(dynamic);
                        row = commentMapper.delete(comment) + row;
                    }
                }
            }
        }
        return row;
    }


}
